import {
    createRouter,
    createWebHashHistory
} from 'vue-router';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
            path: "/",
            name: "index",
            redirect: '/login'
        }, {
            path: "/home",
            name: "Home",
            component: () => import('@/views/Home.vue'),
            redirect:"/home/situation",
            children: [
                {
                    path: "main",
                    name: "Main",
                    component: () => import('@/views/Main.vue'),
                    meta:{
                        bg_on: false
                    }
                },
                {
                    path: "situation",
                    name: "Situation",
                    component: () => import('@/views/Situation.vue'),
                    meta:{
                        bg_on : true
                    }
                },               
                {
                    path: "analyse",
                    name: "Analyse",
                    component: () => import('@/views/Analyse.vue'),
                    meta:{
                        bg_on: false
                    }
                },
                {
                    path: "disposition",
                    name: "Disposition",
                    component: () => import('@/views/Disposition.vue'),
                    meta:{
                        bg_on: false
                    }
                },
                {
                    path: "monitoring",
                    name: "Monitoring",
                    component: () => import('@/views/Monitoring.vue'),
                    meta:{
                        bg_on: false
                    }
                },
                {
                    path: "property",
                    name: "Property",
                    component: () => import('@/views/Property.vue'),
                    meta:{
                        bg_on: false
                    }
                },
                {
                    path: "report",
                    name: "Report",
                    component: () => import('@/views/Report.vue'),
                    meta:{
                        bg_on: false
                    }
                }
                
            ]
        },
        {
            path: "/login",
            name: "Login",
            component: () => import('@/views/Login.vue')
        },
        {
            path: "/situations",
            name: "Situations",
            component: () => import('@/views/Situation.vue')
        },
        {
            path: '/404',
            name: '404',
            component: () => import('@/views/404.vue')
        },
        {
            path: '/:pathMatch(.*)',
            redirect: '/404'
        }
    ]
})

router.beforeEach((to, from, next) => {
    const isAuthentucated = localStorage.getItem('login');
    if (to.name !== 'Login' && !isAuthentucated) {
        next({
            name: 'Login'
        })
    } else {
        next()
    }

})

export default router;